package cn.tianyigps.tianyigpsactivetask.dao;

import cn.tianyigps.tianyigpsactivetask.Dto.ActiveStatusDto;
import cn.tianyigps.tianyigpsactivetask.model.EquipmentStatsModel;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import cn.tianyigps.tianyigpspublic.mongo.dao.BaseDaoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WEI.YUAN
 * @Descirption:
 * @Date: 2019/4/8_19:45
 */
@Repository
public class EQCheckModelDaoImpl extends BaseDaoImp<EquipmentModel> implements EQCheckModelDao {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<EquipmentModel> getEQCheckModelByIDs(List<String> ids) {
        Criteria criteria = Criteria.where("equipmentId").in(ids);
        Query query= new Query(criteria);
        List<EquipmentModel> equipmentCheckModels = mongoTemplate.find(query, EquipmentModel.class, "latestEquipmentData");
        return equipmentCheckModels;

    }

    @Override
    public List<EquipmentStatsModel> getEQCheckModelByIDAndTime(List<ActiveStatusDto> activeStatusDtos) {
        List<EquipmentStatsModel> equipmentCheckModels= new ArrayList<>();
        for(ActiveStatusDto activeStatusDto:activeStatusDtos){
            Criteria criteria=Criteria.where("equipmentId").is(activeStatusDto.getEquipmentId()).and("startTime").gte(activeStatusDto.getActiveTime());
            criteria.and("lineStatus").in("1002","2000");
            List<EquipmentStatsModel> equipmentCheckModel = mongoTemplate.find(new Query(criteria), EquipmentStatsModel.class, "equipmentStatsModel");
            if(!CollectionUtils.isEmpty(equipmentCheckModel)){
                equipmentCheckModels.addAll(equipmentCheckModel);
            }
        }
        return equipmentCheckModels;
    }

    @Override
    public void insertBatch(List<EquipmentStatsModel> result, String collectionName) {
        mongoTemplate.insert(result, collectionName);
    }
}
